package org.foreningsgatan.genetics;

import java.io.Serializable;



public class Program<T extends Serializable, S extends Serializable> implements Serializable {
	protected Node<T, S> rootNode;
	protected int age;
	protected ResultSet resultSet;
	
	public Program(Node<T, S> rootNode) {
		this.rootNode = rootNode;
		this.age = 0;
	}
	
	public Node<T, S> getRootNode() {
		return rootNode;
	}
	
	public ResultSet getResultSet() {
		return resultSet;
	}
	
	public void resetResultSet() {
		this.resultSet = new ResultSet();
		resultSet.put(DefaultResultType.NodeLength, rootNode.count());
	}
	
	public void run(SimulationEnvironment<T, S> environment) {
		resetResultSet();
		
		SimulationEnvironment<T, S> clone = environment.cloneAndReset();
		
		while (clone.simulationActive()) {
			rootNode.perform(clone);
			clone.act();
		}
		
		clone.applyResult(resultSet);
	}
}
